package com.yuyou.activity.group.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.activity.comm.service.service.ISmsService;
import com.yuyou.activity.group.service.lock.MapLock;
import com.yuyou.activity.group.service.service.IActivityGroupService;
import com.yuyou.activity.group.service.vo.req.ReqAttendActivityGroup;
import com.yuyou.activity.group.service.vo.resp.RespActivityGroupDetail;
import com.yuyou.activity.group.service.vo.resp.RespCanActivityGroupList;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.bean.ReturnResult;
import com.yuyou.activity.util.util.SpringUtil;


@Api(value="ActivityGroupController",description="活动拼团接口")
@Validated
@RestController
@RequestMapping(value="/api/activityGroup")
public class ActivityGroupController extends BaseController{

	@Autowired
	private ISmsService smsService;
	
	@Autowired
	private IActivityGroupService activityGroupService;
		
	@ApiOperation(value="推荐拼团列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true)})
	@RequestMapping(value="/findCanActivityGroup",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<RespCanActivityGroupList>> findCanActivityGroup(HttpServletRequest request,@NotBlank(message="项目代码不能为空") String projectCode) throws Exception{
		this.valAllClose(projectCode);		
		List<RespCanActivityGroupList> list=activityGroupService.findCanActivityGroup(projectCode);
		return ReturnResult.success(list);
	}
	
	@ApiOperation(value="推荐拼团列表更多")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
		@ApiImplicitParam(name = "limit", value = "最大数量(1-10)", paramType = "query", required = true)})
	@RequestMapping(value="/findMoreCanActivityGroup",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<RespCanActivityGroupList>> findMoreCanActivityGroup(HttpServletRequest request,
			@NotBlank(message="项目代码不能为空") String projectCode,
			@NotNull(message="最大数量不能为空")Integer limit) throws Exception{
		
		if(limit.intValue()<1 && limit.intValue()>10){
			throw new Exception("limit参数不正确");
		}
		
		this.valAllClose(projectCode);		
		List<RespCanActivityGroupList> list=activityGroupService.findMoreCanActivityGroup(projectCode,limit);
		return ReturnResult.success(list);
	}
	
	@ApiOperation(value="是否可以参团")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
		@ApiImplicitParam(name = "phone", value = "手机号码", paramType = "query", required = true)})
	@RequestMapping(value="/canAttActGro",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<ResBoolSimpleInfo<RespActivityGroupDetail>> canAttActGro(
			HttpServletRequest request,
			@NotBlank(message="项目代码不能为空") String projectCode,
			@NotBlank (message="手机不能为空") String phone ) throws Exception{
		this.valAllClose(projectCode);
		ResBoolSimpleInfo<RespActivityGroupDetail> res=activityGroupService.canAttActGro(projectCode,phone);
		return ReturnResult.returnRes(res);
	}
	
	@ApiOperation(value="提交资料")
	@RequestMapping(value="/attendActivityGroup",method = {RequestMethod.POST})
	public ReturnResult<ResBoolSimpleInfo<RespActivityGroupDetail>> attendActivityGroup(
			HttpServletRequest request , @Validated @ModelAttribute ReqAttendActivityGroup reqAttendActivityGroup) throws Exception{
		
		ResBoolSimpleInfo<RespActivityGroupDetail> resDet=new ResBoolSimpleInfo<RespActivityGroupDetail>();	
		this.valAllClose(reqAttendActivityGroup.getProjectCode());
        //验证短信校验码
		ResBoolSimpleInfo<String> res = smsService.validate(reqAttendActivityGroup.getPhone(),"01", reqAttendActivityGroup.getCode());
		
		//测试环境配置
		Environment env=(Environment)SpringUtil.getBean("environment");
		String messageOpen=env.getProperty("message.open");
		if(messageOpen.equals("off")){						
			resDet=attendActivityGroup(reqAttendActivityGroup);
		}else{
			if(res.isSuccess()){				
				resDet=attendActivityGroup(reqAttendActivityGroup);				
			}else{
				resDet.setMessage(res.getMessage());
				resDet.setSuccess(false);
			}
		}					
		return ReturnResult.returnRes(resDet);
	}
	
	private ResBoolSimpleInfo<RespActivityGroupDetail> attendActivityGroup(ReqAttendActivityGroup reqAttendActivityGroup) throws Exception{
		ResBoolSimpleInfo<RespActivityGroupDetail> resDet=new ResBoolSimpleInfo<RespActivityGroupDetail>();	
		
//		resDet=activityGroupService.attendActivityGroup(reqAttendActivityGroup);
//		return resDet;
		
		try{
		boolean lockRes=MapLock.trylock(reqAttendActivityGroup.getProjectCode()+reqAttendActivityGroup.getPhone());
		if(lockRes){
			resDet=activityGroupService.attendActivityGroup(reqAttendActivityGroup);
			return resDet;
		}
		}finally{
			MapLock.removeLock(reqAttendActivityGroup.getProjectCode()+reqAttendActivityGroup.getPhone());
		}
		resDet.setSuccess(false);
		resDet.setMessage("系统异常,请联系管理员");
		return resDet;
	}
	
	
	@ApiOperation(value="开团情况")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
		@ApiImplicitParam(name = "activityGroupId", value = "拼团主键", paramType = "query", required = false)})
	@RequestMapping(value="/actGroDetById",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<RespActivityGroupDetail> actGroDetById(
			HttpServletRequest request,
			@NotBlank(message="项目代码不能为空") String projectCode,
			@NotNull (message="ID") Long activityGroupId ) throws Exception{
		this.valAllClose(projectCode);
		ResBoolSimpleInfo<RespActivityGroupDetail> res=activityGroupService.activityGroupDetailById(projectCode,activityGroupId);
		return ReturnResult.returnRes(res);
	}
	
	@ApiOperation(value="手机号码查询开团情况")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
		@ApiImplicitParam(name = "phone", value = "手机号码", paramType = "query", required = false)})
	@RequestMapping(value="/actGroDetByPhone",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<RespActivityGroupDetail> actGroDetByPhone(
			HttpServletRequest request,
			@NotBlank(message="项目代码不能为空") String projectCode,
			@NotBlank (message="手机不能为空") String phone ) throws Exception{
		this.valAllClose(projectCode);
		ResBoolSimpleInfo<RespActivityGroupDetail> res=activityGroupService.activityGroupDetail(projectCode,phone);
		return ReturnResult.returnRes(res);
	}
	
	
	
	
	
	
}
